package com.rpframework.website.device.act.admin;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.rpframework.utils.Pager;
import com.rpframework.website.device.domain.Cash;
import com.rpframework.website.device.domain.User;
import com.rpframework.website.device.service.CashService;
import com.rpframework.website.device.service.UserService;

@Controller
@RequestMapping("/admin/cash")
public class AdminCashAct extends AdminAct{
	@Resource CashService service;
	@Resource UserService userService;
	/**
	 * 列表
	 * */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping("/list")
	public String list(
			@RequestParam(value="pager", required=false) Pager pager, 
			@RequestParam(value="status", required=false) Integer status,//1待审核 2失败 3成功 
			Map<Object, Object> model, RedirectAttributes attr){
		if(pager == null) {
			pager = new Pager();
		}
		pager = service.getPager(pager);
		model.put("pager", pager);
		return this.doPackageURI("cash/list");
	}
	@RequestMapping("/apply")
	public String apply(
			@RequestParam(value="id", required=false) Integer id,//1待审核 2失败 3成功 
			@RequestParam(value="status", required=false) Integer status,//1待审核 2失败 3成功 
			Map<Object, Object> model, RedirectAttributes attr){
		Cash t = service.select(id);
		if(t!=null){
			t.setStatus(status);
			boolean flag = service.update(t);
			if(flag){
				if(t.getStatus()==2){
					//扣除余额要退回 
					User u = userService.select(t.getUserId());
					if(u!=null){
						u.setBalance(u.getBalance()+t.getMoney());
						flag =	userService.update(u);
						if(flag)setInfoMsg("操作成功,余额退回!", attr);
						else setInfoMsg("操作失败!", attr);
					}
				}
				if(flag)setInfoMsg("操作成功", attr);
				else setInfoMsg("操作失败!", attr);
			}else{
				setInfoMsg("操作失败!", attr);
			}
		}
		return redirect("/admin/cash/list");
	}

}
